package com.zs.toolbox.web2j.system.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.toolbox.web2j.common.web.WtCommonPropertiesSetter;
import com.zs.toolbox.web2j.system.core.entity.*;
import com.zs.toolbox.web2j.system.core.mapper.other.WtPermissionMapper;
import com.zs.toolbox.web2j.system.core.mapper.other.WtRoleMapper;
import com.zs.toolbox.web2j.system.core.mapper.other.WtRolePermissionMapper;
import com.zs.toolbox.web2j.system.core.mapper.user.WtUserRoleMapper;
import com.zs.toolbox.web2j.system.core.service.WtRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台角色表 服务实现类
 * </p>
 *
 * @author eric.zhang
 * @since 2018-09-26
 */
@Service
public class WtRoleServiceImpl extends ServiceImpl<WtRoleMapper, WtRoleEntity> implements WtRoleService {
    @Autowired
    WtUserRoleMapper wtUserRoleMapper;

    @Autowired
    WtRolePermissionMapper wtRolePermissionMapper;

    @Autowired
    WtPermissionMapper wtPermissionMapper;

    @Override
    public List<WtRoleEntity> selectUserRolesByUserId(Long id) {
        List<WtUserRoleEntity> list = wtUserRoleMapper
                .selectList(new LambdaQueryWrapper<WtUserRoleEntity>().eq(WtUserRoleEntity::getUserId, id));
        if (CollectionUtils.isNotEmpty(list)) {
            List<WtRoleEntity> wtRoleEntities = baseMapper
                    .selectBatchIds(list.stream().map(WtUserRoleEntity::getRoleId)
                            .collect(Collectors.toList()));
            return wtRoleEntities;
        } else {
            return ListUtils.EMPTY_LIST;
        }
    }

    @Override
    public WtRoleWithPermissionEntity getRoleWithPermissionById(Long id) {

        WtRoleEntity wtRoleEntity = getById(id);
        if (null != wtRoleEntity) {
            List<Long> permissionIds = wtRolePermissionMapper.selectList
                    (new LambdaQueryWrapper<WtRolePermissionEntity>().eq
                            (WtRolePermissionEntity::getRoleId, wtRoleEntity.getId()))
                    .stream().map(WtRolePermissionEntity::getPermissionId).collect(Collectors.toList());

            Collection<WtPermissionEntity> permissionEntities = wtPermissionMapper
                    .selectBatchIds(permissionIds);

            WtRoleWithPermissionEntity wtRoleWithPermissionEntity =
                    new WtRoleWithPermissionEntity().setPermissions(permissionEntities);
            BeanUtils.copyProperties(wtRoleEntity, wtRoleWithPermissionEntity);
            return wtRoleWithPermissionEntity;
        } else {
            return null;
        }
    }

    @Override
    @Transactional
    public void saveRolePermission(Long roleId, List<Long> permissionIds, Long userId) {

        /*删除原有角色权限*/
        wtRolePermissionMapper.deleteById(roleId);

        /*提取出真实存在的permissionId*/
        Collection<WtPermissionEntity> permissionEntities = wtPermissionMapper
                .selectBatchIds(permissionIds);

        /*添加新权限*/
        permissionEntities.forEach(permissionEntity -> {
            WtRolePermissionEntity wtRolePermissionEntity = new WtRolePermissionEntity();
            WtCommonPropertiesSetter.setDefaultCommonProperty(wtRolePermissionEntity, userId);
            wtRolePermissionEntity.setRoleId(roleId).setPermissionId(permissionEntity.getId());
            wtRolePermissionMapper.updateById(wtRolePermissionEntity);
        });
    }

}
